package cn.oowoo.rabbitmq.dlx;

import cn.oowoo.rabbitmq.config.RabbitConfig;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

/**
 * @Author: Zhangbin
 * @Date: 2021/5/9 14:55
 * @Description:  测试DLX-死信生产者，这里生产的消息有可能被正常消费，也有可能进入死信队列
 **/
public class DlxProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitConfig.createConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder()
                 .deliveryMode(2)
                .contentEncoding("UTF-8")
                .contentType("text/plain");

        for(int i=0;i<10;i++){
            String msg = "测试DLX消息.... "+i;
            if( i == 3 || i== 6 || i==9){
                builder.expiration("500"); //500毫秒，如果没有消费者消费，超时消息将会被路由到死信队列
            }else{
                builder.expiration("9999999");
            }
            AMQP.BasicProperties properties = builder.build();
            channel.basicPublish(RabbitConfig.EXCHANGE_DLX_NONE_NAME,RabbitConfig.ROUTING_DLX_NONE_KEY,properties,msg.getBytes());
        }
        //消息发送完毕关闭信道，关闭链接
        channel.close();
        connection.close();
    }
}
